package com.zhongxi.guoyuan.adapter.seller;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.seller.ItemOrderInfo;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.UiUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/18.
 */

public class PostitemOrderListAdapter extends BaseAdapter {
    private ItemOrderInfo itemOrderInfo;

    public PostitemOrderListAdapter(ItemOrderInfo itemOrderInfo) {
        this.itemOrderInfo = itemOrderInfo;
    }

    @Override
    public int getCount() {
        return 1;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null) {
            convertView = UiUtils.inflate(R.layout.item_order_list_seller_dd);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        if (1==itemOrderInfo.ordertype){
            convertView.setBackgroundColor(UiUtils.getContext().getResources().getColor(R.color.cart_background));
        }else {
            convertView.setBackgroundColor(UiUtils.getContext().getResources().getColor(R.color.bg_blue));
        }
        Glide.with(UiUtils.getContext()).load(Url.IMG_URL + itemOrderInfo.thumbnail).placeholder(R.mipmap.iv_loading).into(vh.mIvImg);
        vh.mTvName.setText(itemOrderInfo.goodsname);
        vh.mTvNum.setText("X " + itemOrderInfo.goodsnum);
        if (itemOrderInfo.gattr != null && !"".equals(itemOrderInfo.gattr)) {
            String gattr = itemOrderInfo.gattr;
            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(gattr);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Iterator<String> keys = jsonObject.keys();
            String attr = "";
            while (keys.hasNext()) {
                String next = keys.next();
                String s = jsonObject.optString(next);
                attr += s + "/";
            }
            vh.mTvAttr.setText(attr.substring(0, attr.length() - 1));
        } else {
            vh.mTvAttr.setText("");
        }
        vh.mTvMd.setText(itemOrderInfo.innerunitprice + " 美豆");
        vh.mTvHd.setText("+" + itemOrderInfo.redbeanunitprice + " 红豆");
        return convertView;
    }


    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    class ViewHolder {
        @Bind(R.id.ivImg)
        ImageView mIvImg;
        @Bind(R.id.tvName)
        TextView mTvName;
        @Bind(R.id.tvNum)
        TextView mTvNum;
        @Bind(R.id.tvAttr)
        TextView mTvAttr;
        @Bind(R.id.tv_hd)
        TextView mTvHd;
        @Bind(R.id.tv_md)
        TextView mTvMd;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
